<?php
/**
 * Hellos View for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/
 * @license		GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.view' );

jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');

class JanewsViewlanguages extends JView
{
	/**
	 * Hellos view display method
	 * @return void
	 **/
	function display($tpl = null){
		$task = isset($_REQUEST['task'])?$_REQUEST['task']:'';
		if($this->getLayout() == 'form') {
			$this->_displayForm($tpl, $task);
			return;
		}
		switch ($task){			
			case 'edit':
				$this->_displayForm($tpl, $task);
				break;				
			default:
				$this->view($tpl, $task);
		}
	}
	
	function _toolbarDefault($edit=false)
	{
	  JToolBarHelper::title(   JText::_( 'LANGUAGE MANAGER' ), 'generic.png' );		
	  JToolBarHelper::custom('edit','edit', '', JText::_('Edit'), true, false);
	}
	  
	function _toolbarEdit($edit=false)
	{
	  JRequest::setVar( 'hidemainmenu', 1 );
		JToolBarHelper::title( JText::_( 'EDIT LANGUAGE' ));
		JToolBarHelper::custom('apply', 'apply', '', 'Apply', false);
		JToolBarHelper::custom('save', 'save', '', 'Save', false);
		JToolBarHelper::custom('cancel', 'cancel', '', 'Cancel', false);
	}
	
	function _displayForm($tpl, $task){
		$option = JRequest::getWord('option');
		$lang = JRequest::getVar('lang', '');
		$client = & JApplicationHelper::getClientInfo(JRequest::getVar('client', '1', '', 'int'));
		$path_dest = JLanguage::getLanguagePath($client->path);
		$path = $path_dest.DS.$lang.DS.$lang.'.'.$option.'.ini';
		$data = '';
		
		if($lang!='' && JFile::exists($path)){
			$data = JFile::read($path);			
		}
		else {
			if(JFile::exists($path_dest.DS.'en-GB'.DS.'en-GB'.'.'.$option.'.ini')){
				$data = JFile::read($path_dest.DS.'en-GB'.DS.'en-GB'.'.'.$option.'.ini');
			}	
			
		}
		$file = $lang.'.'.$option.'.ini';
		
		$this->assignRef('data', $data);
		$this->assignRef('filename', $file);
		$this->assignRef('lang', $lang);
		$this->assignRef('client', $client);
		$this->assignRef('path_lang', $path_dest);
		$this->assignRef('task', $task);
		
		parent::display($tpl);
	}
	/**
	* Compiles a list of installed languages
	*/
	function view($tpl, $task)
	{
		global $mainframe, $option;

		// Initialize some variables
		$db		=& JFactory::getDBO();
		$client	=& JApplicationHelper::getClientInfo(JRequest::getVar('client', '0', '', 'int'));
		$rows	= array ();			
		
		$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );
	
		//load folder filesystem class		
		
		$path = JLanguage::getLanguagePath($client->path);
		$dirs = JFolder::folders( $path );
		$i = 0; $data['name'] = '';
		foreach ($dirs as $dir)
		{
			
				$files = JFolder::files( $path.DS.$dir, '^([-_A-Za-z]*)\.xml$' );
				foreach ($files as $file)
				{
					$data = JApplicationHelper::parseXMLLangMetaFile($path.DS.$dir.DS.$file);
					$row 			= new StdClass();
					$row->id 		= $i;
					$row->language 	= substr($file,0,-4);
			
					if (!is_array($data)) {
						continue;
					}
					foreach($data as $key => $value) {
						$row->$key = $value;
					}
					
					// if current than set published
					$params = JComponentHelper::getParams('com_languages');
					if ( $params->get($client->name, 'en-GB') == $row->language) {
						$row->published	= 1;
					} else {
						$row->published = 0;
					}
					
					$row->checked_out = 0;
					$row->mosname = JString::strtolower( str_replace( " ", "_", $row->name ) );
					$rows[] = $row;
				}
				$i++;
			
		}	

		jimport('joomla.html.pagination');
		$pageNav = new JPagination( $i, $limitstart, $limit );

		$rows = array_slice( $rows, $pageNav->limitstart, $pageNav->limit );

		$this->assignRef('rows', $rows);
		$this->assignRef('page', $pageNav);
		$this->assignRef('client', $client);
		$this->assignRef('task', $task);
		
		parent::display($tpl);	
	}

}